package com.models;

import com.util.Error;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Created by TimeLiar on 2015/9/22.
 * Speed
 */
public class TableData implements Serializable {
    private Table table;
    private ArrayList<ArrayList<String>> rows;

    public TableData(Table table) {
        this.table = table;
        rows = new ArrayList<>();
    }

    public Table getTable() {
        return table;
    }

    public void setTable(Table table) {
        this.table = table;
    }

    public int getColCount() {
        if(table == null || table.getColumns() == null){
            return -1;
        }
        return table.getColumns().size();
    }

    public ArrayList<ArrayList<String>> getRows() {
        return rows;
    }

    @SuppressWarnings("unused")
    public void setRows(ArrayList<ArrayList<String>> rows) {
        this.rows = rows;
    }

    public void insertRow(ArrayList<String> row) throws com.util.Error {
        if (row == null || row.size() != getColCount()) {
            throw new Error("invalid row");
        }
        ArrayList<Column> columns = table.getColumns();
        for (int i = 0; i < columns.size(); i++) {
            Column column = columns.get(i);
            if (row.get(i) != null) {
                switch (columns.get(i).getType().toLowerCase()) {
                    case "int": {
                        if (!isNumeric(row.get(i)) || row.get(i).length() > column.getLength()) {
                            throw new Error(column, row.get(i));
                        }
                        break;
                    }
                    case "char":
                    case "string":
                    case "text":
                    case "varchar": {
                        if (row.get(i).length() > column.getLength()) {
                            throw new Error(column, row.get(i));
                        }
                        break;
                    }
                    case "double": {
                        if (!isDouble(row.get(i)) || row.get(i).length() > column.getLength()) {
                            throw new Error(column, row.get(i));
                        }
                        break;
                    }
                    default:
                        throw new Error();
                }
                if(column.isPrimaryKey()){
                    for(ArrayList<String> temp:rows){
                        if(row.get(i).equals(temp.get(i))){
                            throw new Error(column.getName() + " is Primary Key");
                        }
                    }
                }
            } else {
                if(column.isNotNull()){
                    throw new Error(column, row.get(i));
                }
            }
        }
        rows.add(row);
    }

    public boolean isNumeric(String token) {
        if (token == null) {
            return false;
        }
        for (char c : token.toCharArray()) {
            if (!Character.isDigit(c)) {
                return false;
            }
        }
        return true;
    }

    public boolean isDouble(String token) {
        if (token == null) {
            return false;
        }
        int dot = 0;
        for (char c : token.toCharArray()) {
            if (c == '.') {
                dot++;
            } else {
                if (!Character.isDigit(c)) {
                    return false;
                }
            }
        }
        return dot == 1;
    }

    /*@Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for(String[] row:rows){
            builder.append(Arrays.toString(row)).append("\r\n");
        }
        return builder.toString();
    }*/
}
